﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.CommonImp;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class IncomeControl : UserControl
    {
        private readonly IIncomeService<Income> _income;

        public IncomeControl(IIncomeService<Income> icome)
        {
            _income = icome;

            InitializeComponent();
        }

        private void IncomeControl_Load(object sender, EventArgs e)
        {
            var income = new IncomeService();
            comboBox1.DataSource = null;
            List<IncomeType> getincome = income.GetName();
            comboBox1.DataSource = getincome;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Id";

            GetAll();
        }

        private void btnIncomeInsert_Click(object sender, EventArgs e)
        {
            var form = new IncomeAddForm(new IncomeService())
            {
                RefreshMainForm = GetAll
            };
            form.ShowDialog(this);
        }

        private void btnIncomeUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show(@"未选择");
                return;
            }
            var getcell = dataGridView1.CurrentRow.Index;
            var income = new Income
            {
                Id = long.Parse(dataGridView1.Rows[getcell].Cells["Id"].Value.ToString()),
                Money = decimal.Parse(dataGridView1.Rows[getcell].Cells["Money"].Value.ToString()),
                Comment = dataGridView1.Rows[getcell].Cells["Comment"].Value.ToString(),
                IncomeTypeId = Convert.ToInt32(dataGridView1.Rows[getcell].Cells["IncomeTypeId"].Value.ToString()),
                AccountTypeId = Convert.ToInt32(dataGridView1.Rows[getcell].Cells["AccountTypeId"].Value.ToString()),
            };

            var form = new IncomeUpdateForm(new IncomeService())
            {
                Income = income,
                RefreshUpdateMainForm = GetAll
            };
            form.ShowDialog(this);
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show(@"未选择");
                return;
            }
            var getcell = dataGridView1.CurrentRow.Index;
            _income.Delete(Convert.ToInt32(dataGridView1.Rows[getcell].Cells[0].Value.ToString()));
            GetAll();
        }

        private void IncomeSelect_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(IncomeId.Text))
            {
                MessageBox.Show(@"请输入账户 --");
                return;
            }
            long id = long.Parse(IncomeId.Text);

            if (_income.Get(id) != null)
            {
                var get = new List<Income> {_income.Get(id)};
                dataGridView1.DataSource = get.ToArray();
            }
        }

        private void incomeSource_Click(object sender, EventArgs e)
        {
            long incomesource = long.Parse(comboBox1.SelectedValue.ToString());
            var income = new IncomeService();
            if (income.GetDim(incomesource) != null)
            {
                List<Income> getdim = income.GetDim(incomesource);
                dataGridView1.DataSource = getdim;
            }
        }

        private void btnMoney_Click(object sender, EventArgs e)
        {
            decimal incomesource = decimal.Parse(txtIncomeMoney.Text);
            var income = new IncomeService();
            if (income.GetDim(incomesource) != null)
            {
                List<Income> getdim = income.GetDim(incomesource);
                dataGridView1.DataSource = getdim;
            }
        }

        public void GetAll()
        {
            dataGridView1.DataSource = null;
            var re = new DataGridViewService();
            dataGridView1.Columns.AddRange(re.BindColumns(typeof (Income)));
            List<Income> getall = _income.GetAll();
            dataGridView1.DataSource = getall.ToArray();
        }
    }
}